#pragma once
#include "player.h"
#include "pea_bullet.h"

extern Atlas atlas_peashooter_idle_left;
extern Atlas atlas_peashooter_idle_right;
extern Atlas atlas_peashooter_run_left;
extern Atlas atlas_peashooter_run_right;
extern Atlas atlas_peashooter_attack_ex_left;
extern Atlas atlas_peashooter_attack_ex_right;
extern Atlas atlas_peashooter_die_left;
extern Atlas atlas_peashooter_die_right;

extern std::vector<Bullet*> bullet_list;
extern Camera main_camera;

class PeashooterPlayer : public player
{
public:
	PeashooterPlayer()
	{
		animation_idle_left.set_atlas(&atlas_peashooter_idle_left);
		animation_idle_right.set_atlas(&atlas_peashooter_idle_right);
		animation_run_left.set_atlas(&atlas_peashooter_run_left);
		animation_run_right.set_atlas(&atlas_peashooter_run_right);
		animation_attack_ex_left.set_atlas(&atlas_peashooter_attack_ex_left);
		animation_attack_ex_right.set_atlas(&atlas_peashooter_attack_ex_right);
		animation_die_left.set_atlas(&atlas_peashooter_die_left);
		animation_die_right.set_atlas(&atlas_peashooter_die_right);

		animation_attack_ex_left.set_interval(100);
		animation_attack_ex_right.set_interval(100);
		animation_idle_left.set_interval(100);
		animation_idle_right.set_interval(100);
		animation_run_left.set_interval(100);
		animation_run_right.set_interval(100);
		animation_die_left.set_interval(150);
		animation_die_right.set_interval(150);

		animation_die_left.set_loop(false);
		animation_die_right.set_loop(false);

		size = { 96,96 };

		timer_attack_ex.set_wait_time(attack_ex_duration);
		timer_attack_ex.set_one_shot(true);
		timer_attack_ex.set_callback([&]() {is_attack_ex = false; });

		timer_spwan_pea_ex.set_wait_time(100);
		timer_spwan_pea_ex.set_callback([&]() {spawn_pea_bullet(speed_pea_ex); });

		attack_cd = 100;
	}
	~PeashooterPlayer() = default;
	 
	void on_update(int delta)
	{
		player::on_update(delta);
		if (is_attack_ex)
		{
			main_camera.shake(5, 100);
			timer_attack_ex.on_update(delta);
			timer_spwan_pea_ex.on_update(delta);
			is_facing_right ? animation_attack_ex_right.on_update(delta) :
				animation_attack_ex_left.on_update(delta);
		}
		else
			mciSendString(L"stop pea_shoot_ex", NULL, 0, NULL);
	}
	void on_attack()
	{
		spawn_pea_bullet(speed_pea);

		switch (rand() % 2)
		{
		case 0:
			mciSendString(L"play pea_shoot_1 from 0", NULL, 0, NULL);
			break;
		case 1:
			mciSendString(L"play pea_shoot_2 from 0", NULL, 0, NULL);
			break;
		}
	}
	void on_attack_ex()
	{
		is_attack_ex = true;
		timer_attack_ex.rerstart();

		is_facing_right ? animation_attack_ex_right.reset() : animation_attack_ex_left.reset();
	
		mciSendString(L"play pea_shoot_ex from 0", NULL, 0, NULL);
	}
	void on_draw(const Camera& camera)
	{
		if (is_attack_ex)
			is_facing_right ? animation_attack_ex_right.on_draw(camera, (int)get_position().x, (int)get_position().y) :
			animation_attack_ex_left.on_draw(camera, (int)get_position().x, (int)get_position().y);
		else
			player::on_draw(camera);
	}
private:
	const float speed_pea = 0.75f;
	const float speed_pea_ex = 1.5f;
	const float attack_ex_duration = 2500;

	Timer timer_attack_ex;
	Timer timer_spwan_pea_ex;

	void spawn_pea_bullet(float speed)
	{
		Bullet* bullet = new PeaBullet();
		Vector2 bullet_position, bullet_speed;
		const Vector2 bullet_size = bullet->get_size();
		bullet_position.x = is_facing_right
			? position.x + size.x - bullet_size.x / 2
			: position.x - bullet_size.x / 2;
		bullet_position.y = position.y;
		bullet_speed = { is_facing_right ? speed : -speed, 0 };

		bullet->set_position(bullet_position.x, bullet_position.y);
		bullet->set_speed(bullet_speed.x, bullet_speed.y);

		bullet->set_collide_target(id == PlayerID::P1 ? PlayerID::P2 : PlayerID::P1);
		bullet->set_callback([&]() {mp += 25; });
		
		bullet_list.push_back(bullet);
	}
};